// pages/mine/other/message.js
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    boxMsg:{
      content: '暂无消息'
    },
    replenishmentMsg: {
      content: '暂无消息'
    }, 
    lingshiMsg:  {
      content: '暂无消息'
    }, 
    chaokuMsg: {
      content: '暂无消息'
    },
    shenghuoMsg: {
      content: '暂无消息'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/message/user/getlist',
      data: {
        token: wx.getStorageSync('token'),
        start: 0,
        num: 1000,
        type:''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          // -1 潮酷商城消息 -2 生活服务订单消息 -3 点赞 -4 关注  -5 收藏  -6 评论 猫盒： 0 系统消息   1 零食直购订单消息 2 补货商城订单消息 3 盒子消息  4 优惠券
          let list = res.data.data;
          list.forEach((item)=>{
            item.update_time = App.timeTransformat(item.update_time);
            switch(item.type){
              case '1': //零食直购
                that.setData({
                  lingshiMsg: item
                })
                break;
              case '2': //补货
                that.setData({
                  replenishmentMsg: item
                })
                break;
              case '3': //盒子消息
                that.setData({
                  boxMsg: item
                })
                break;
              case '-1': //潮酷商城
                that.setData({
                  chaokuMsg: item
                })
                break;
              case '-2': //生活服务订单
                that.setData({
                  shenghuoMsg: item
                })
                break;
              default:
            }
          })
          wx.hideLoading();
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration: 800
          })
        }
      },fail(){
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 已读消息设置
  setReadMsg:function(e){
    let msgType = e.currentTarget.dataset.type;
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/message/user/browser',
      data: {
        token: wx.getStorageSync('token'),
        type: msgType
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        
      }
    })
  },

  // 盒子消息详情
  goBoxDetail:function(e){
    if (this.data.boxMsg.source_id != undefined) {
      wx.reLaunch({
        url: '/pages/index/index?boxId=' + this.data.boxMsg.source_id
      })
      this.setReadMsg(e);
    } 
  },

  // 补货详情
  goSupplyDetail:function(e){
    if (this.data.replenishmentMsg.source_id==undefined){
      return false;
    }
    let type = this.data.replenishmentMsg.attach.spec;
    let supplyType = 0;//补货状态
    switch (type){
      case 13:  //13补货下单成功
        supplyType = 0;
        break;
      case 14: // 补货单已确认
        supplyType = 1;
        break;
      case 15:  //配送中
        supplyType = 2;
        break;
      case 16: //已送达
        supplyType = 3;
        break;
      case 18: //补货单已取消
      case 19:
        supplyType = 4;
        break;
      default:
    }
    wx.navigateTo({
      url: '/pages/mine/order/order_detail?order_no=' + this.data.replenishmentMsg.source_id + '&type=' + supplyType
    })
    this.setReadMsg(e);
  },

  // 零食直购
  goZhigouDetail:function(e){
    if (this.data.lingshiMsg.source_id != undefined) {
      wx.navigateTo({
        url: '/pages/mine/lingshi_order/lingshi_detail?order_no=' + this.data.lingshiMsg.source_id
      })
      this.setReadMsg(e);
    } 
  },

  // 去超酷商城
  goChaokuOrder:function(e){
    if (this.data.chaokuMsg.source_id != undefined){
      let pageUrl = App.data.globalMall + '/#/order_detail?order_no=' + this.data.chaokuMsg.source_id + '&token=' + wx.getStorageSync('token') + '&openid=' + wx.getStorageSync('userInfo').openid
      wx.navigateTo({
        url: '/pages/apply/web_page?url=' + encodeURIComponent(pageUrl)
      })
      this.setReadMsg(e);
    } 
  },

  // 去生活服务详情
  goServerOrder: function (e) {
    if (this.data.shenghuoMsg.source_id != undefined) {
      let pageUrl = App.data.globalMall + '/#/tg_order_detail?order_no=' + this.data.shenghuoMsg.source_id + '&token=' + wx.getStorageSync('token') + '&openid=' + wx.getStorageSync('userInfo').openid
      wx.navigateTo({
        url: '/pages/apply/web_page?url=' + encodeURIComponent(pageUrl)
      })
      this.setReadMsg(e);
    }
  },

  // 评论详情
  goPinglunPage:function(){
    let pageUrl = App.data.globalUGC + '/commentNews?token=' + wx.getStorageSync('token')
    wx.navigateTo({
      url: '/pages/apply/web_page?url=' + encodeURIComponent(pageUrl)
    })
  },

  // 粉丝详情
  goFansPage:function(){
    let pageUrl = App.data.globalUGC + '/followNews?token=' + wx.getStorageSync('token')
    wx.navigateTo({
      url: '/pages/apply/web_page?url=' + encodeURIComponent(pageUrl)
    })
  },

  // 点赞
  goZanPage: function () {
    let pageUrl = App.data.globalUGC + '/likeNews?token=' + wx.getStorageSync('token')
    wx.navigateTo({
      url: '/pages/apply/web_page?url=' + encodeURIComponent(pageUrl)
    })
  }
})